package cn.edu.ujn.finallab.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.annotation.JsonAppend.Attr;

import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.Room;
import cn.edu.ujn.finallab.service.IOrderService;
import cn.edu.ujn.finallab.service.IRoomService;

@Controller
public class CheckOutController {

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IRoomService roomService;

	// 办理退房登记
	@RequestMapping("/checkOut")
	public String chenkOut(Model model) {
		List<Orders> list = new ArrayList<Orders>();
		model.addAttribute("orderLost", list);
		model.addAttribute("size", -1);
		return "checkOut";
	}

	// 根据房间号查询顾客的订单信息
	@RequestMapping("/selectOrderByNumber")
	public String selectOrderByNumber(String roomNumber, Model model) {
		String state="1";
		Orders order = this.orderService.selectByRoomNumberAndState(roomNumber, state);
		model.addAttribute("order", order);
		model.addAttribute("msg","未查询到该房间的入住信息！");
		return "checkOut";
	}

	// 为顾客办理退房，根据选择的订单号，修改订单的状态，然后查询客房状态
	// 此时客房处于已入住状态

	// 修改客房状态
	// 为顾客办理退房，根据选择的订单号，修改订单的状态，然后查询客房状态
	// 此时客房处于已入住状态
	@RequestMapping("/toCheckOut")
	public String toCheckOut(String orderNumber, Model model) {
		// 查询并修改订单
		Orders order = this.orderService.selectByOrderNumber(orderNumber);
		// 修改订单状态
		// 修改订单
		order.setState("2");
		int updateOrder = this.orderService.updateOrder(order);
		String name = "", outtime = "", intime = "";

		List<Orders> list = this.orderService.selectByMany(order.getRoomNumber(), name, intime, outtime);

		// 查询客房
		Room room = this.roomService.selectByNumber(order.getRoomNumber());
		int flag = 0;
		for (Orders orders : list) {
			if (orders.getState() == "0") {
				flag = 1;
			}
		}
		if ((list != null) && (flag == 1)) {
			room.setState("已预定");
		} else {
			room.setState("未预定");
		}

		// 修改客房

		int updateRoom = this.roomService.updateRoom(room);
		// 都修改完成，返回到原界面
		List<Orders> orderList = this.orderService.selectByIdcard(order.getCustomerIdcard());
		model.addAttribute("orderList", orderList);
		model.addAttribute("size", orderList.size());
		System.out.println(orderList.size());
		System.out.println(orderList);
		return "checkOut";
	}

}


